<template>
  <div class="account">
    <el-collapse v-model="bskdeNames">
      <el-collapse-item title="商品管理介绍" name="1">
        <div>
          商品管理模块可以对客户端的商品进行增删改查等相关操作,来控制客户端商品的列表展示和商品详情
        </div>
      </el-collapse-item>
    </el-collapse>
    <div class="shang">
      <el-collapse v-model="activeNames">
        <el-collapse-item title="条件查询" name="1">
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-row>
              <el-col :span="6">
                <el-form-item label="商品名称">
                  <el-input v-model="formInline.name" placeholder="商品名称" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="商品货号">
                  <el-input
                    v-model="formInline.productSn"
                    placeholder="商品货号"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="品牌">
                  <el-select v-model="formInline.brandId" placeholder="品牌">
                    <el-option
                      v-for="item in listName"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="上架状态">
                  <el-select
                    v-model="formInline.publishStatus"
                    placeholder="上架状态"
                  >
                    <el-option label="上架" value="1" />
                    <el-option label="下架" value="0" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="审核状态">
                  <el-select v-model="formInline.region" placeholder="审核状态">
                    <el-option label="审核通过" value="1" />
                    <el-option label="未审核" value="0" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item>
                  <el-button type="primary" @click="chaxun">查询</el-button>
                  <el-button @click="chonzhi">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-collapse-item>
      </el-collapse>
    </div>
    <div class="xia">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span class="newzeng" @click="newShow">新 增</span>
          <span class="newzeng">导出商品列表excel文件</span>
        </div>
        <template>
          <el-table :data="listersList" border style="width: 100%">
            <el-table-column type="index" label="序列号" width="80" />
            <el-table-column prop="name" label="商品图片" width="180">
              <template v-slot="sisui">
                <div style="width: 100px; height: 100px">
                  <img
                    :src="sisui.row.pic"
                    alt=""
                    style="width: 100%; height: 100%"
                  >
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="商品名称" width="180" />
            <el-table-column prop="price" label="商品价格" width="180">
              <template v-slot="sisui">
                <div>原价:{{ sisui.row.originalPrice }}</div>
                <div>现价:{{ sisui.row.price }}</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="productCategoryName"
              label="商品类别"
              width="100"
            />
            <el-table-column prop="date" label="标签" width="180">
              <template v-slot="sisui">
                <div>
                  最新
                  <el-switch
                    v-model="sisui.row.newStatus"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    @change="zuixin(sisui.row)"
                  />
                </div>
                <div>
                  推荐
                  <el-switch
                    v-model="sisui.row.recommendStatus"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    @change="tuijian(sisui.row)"
                  />
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="date" label="标签2" width="180">
              <template v-slot="sisui">
                <div>
                  发布
                  <el-switch
                    v-model="sisui.row.publishStatus"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    @change="fabu(sisui.row)"
                  />
                </div>
                <div>
                  审核
                  <el-switch
                    v-model="sisui.row.verifyStatus"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    @change="shenhe(sisui.row)"
                  />
                </div>
              </template>
            </el-table-column>
            <el-table-column label="sku" width="100">
              <template v-slot="sisui">
                <el-link type="primary" @click="skuBianji(sisui.row)">编辑sku</el-link>
              </template>
            </el-table-column>
            <el-table-column prop="weight" label="重量" width="100" />
            <el-table-column prop="sort" label="排序" width="100" />
            <el-table-column label="操作" width="100" fixed="right">
              <template v-slot="sisui">
                <el-link
                  type="primary"
                  style="margin-right: 20px"
                  @click="bianji(sisui.row)"
                >编辑</el-link>
                <el-link type="danger" @click="sanchu(sisui.row)">删除</el-link>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-card>
    </div>
    <el-pagination
      :current-page="start"
      :page-sizes="[1, 5, 10, 15]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getSkusByProductId } from '@/api/product/SKU/index.js'
import { findAllBrand } from '@/api/product/judge/index.js'
import {
  productsByPage,
  switchNewStatus,
  switchPublishStatus,
  switchRecommandStatus,
  switchVerifyStatus,
  del
} from '@/api/product/list/index.js'
export default {
  data() {
    return {
      tanSKuList: '',
      // 弹框的显示与隐藏
      dialogTableVisible: false,
      bskdeNames: 1,
      activeNames: 1,
      start: 1,
      limit: 10,
      formInline: {
        brandId: '',
        id: '',
        name: '',
        productCategoryId: '',
        productSn: '',
        publishStatus: '',
        verifyStatus: ''
      },
      // 商品列表数据
      listersList: [],
      // 品牌名称列表数据
      listName: [],
      total: 0
    }
  },
  created() {
    this.init()
    // 获取商品品牌名称
    findAllBrand().then((res) => {
      console.log(res)
      this.listName = res.data.items
    })
  },
  methods: {
    init() {
      productsByPage(this.start, this.limit, this.formInline).then((res) => {
        console.log(res)
        res.data.rows.forEach((ele) => {
          ele.newStatus = !!ele.newStatus
          ele.recommendStatus = !!ele.recommendStatus
          ele.publishStatus = !!ele.publishStatus
          ele.verifyStatus = !!ele.verifyStatus
        })
        this.total = res.data.total
        this.listersList = res.data.rows
      })
    },
    // 点击查询按钮
    chaxun() {
      console.log(111)
      this.init()
    },
    // 点击重置按钮
    chonzhi() {
      this.formInline = {
        brandId: '',
        id: '',
        name: '',
        productCategoryId: '',
        productSn: '',
        publishStatus: '',
        verifyStatus: ''
      }
      this.init()
    },
    // 点击更新最新状态
    zuixin(e) {
      const shuzi = e.newStatus ? '1' : '0'
      console.log(shuzi)
      switchNewStatus({
        productId: e.id,
        status: shuzi
      }).then((res) => {
        console.log(res)
        if (res.success) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.init()
        } else {
          this.$message.error('修改失败')
          this.init()
        }
      })
    },
    // 更改推荐状态
    tuijian(e) {
      const shuzi = e.recommendStatus ? '1' : '0'
      console.log(shuzi)
      switchRecommandStatus({
        productId: e.id,
        status: shuzi
      }).then((res) => {
        console.log(res)
        if (res.success) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.init()
        } else {
          this.$message.error('修改失败')
          this.init()
        }
      })
    },
    // 更改发布状态
    fabu(e) {
      console.log(e)
      const shuzi = e.publishStatus ? '1' : '0'

      switchPublishStatus({
        productId: e.id,
        status: shuzi
      }).then((res) => {
        console.log(res)
        if (res.success) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.init()
        } else {
          this.$message.error('修改失败')
          this.init()
        }
      })
    },
    // 更改审核状态
    shenhe(e) {
      const shuzi = e.verifyStatus ? '1' : '0'
      console.log(shuzi)
      switchVerifyStatus({
        productId: e.id,
        status: shuzi
      }).then((res) => {
        console.log(res)
        if (res.success) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.init()
        } else {
          this.$message.error('修改失败')
          this.init()
        }
      })
    },
    // 点击编辑 sku值
    skuBianji(e) {
      console.log(e.id)
      const id = e.id
      getSkusByProductId(
        id
      ).then(res => {
        console.log(res)
      })
    },
    // 点击删除SKU删除连接
    skuShanChu(e) {
      console.log(e)
    },
    // 点击编辑SKU编辑连接
    skuBianJi(e) {
      console.log(e)
    },
    // 点击编辑
    bianji(e) {
      console.log(e.id)
      // console.log(e.id)
      const id = e.id
      this.$router.push(`/product/copyreader?id=${id}`)
    },
    // 点击删除
    sanchu(e) {
      console.log(e.id)
      const id = e.id
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(id).then(res => {
          this.init()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击页数发送请求新数据
    handleCurrentChange(e) {
      // console.log(e)
      this.start = e
      this.init()
    },
    // 点击条数/页触发请求新数据
    handleSizeChange(e) {
      this.limit = e
      this.init()
    },
    // 点击新增按钮
    newShow() {
      this.$router.push('/product/newshow')
    }
  }
}
</script>

<style lang="scss" scoped>
.account {
  .shang {
    margin: 30px;
  }
  .xia {
    margin: 30px;
    .newzeng{
      color: #fff;
      font-size: 12px;
      margin-right: 20px;
      padding: 10px 20px;
      border-radius: 10px;
      background-color: #7801ff;
    }
  }
}
</style>
